package xcmg.device.service.order;


import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.order.SubcontractingOrderDetailDO;
import xcmg.device.dao.mapper.order.SubcontractingOrderDetailMapper;
import xcmg.device.service.vo.order.SubcontractingOrderDetailVO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * SubcontractingOrderDetail 后台接口实现类
 */
@Slf4j
@Service
public class SubcontractingOrderDetailService {

    @Autowired
    private SubcontractingOrderDetailMapper subcontractingOrderDetailMapper;


    public List<SubcontractingOrderDetailVO> queryList(SubcontractingOrderDetailVO p) {
        return subcontractingOrderDetailMapper.queryList(p);
    }


    /**
     * 查询订单详情并关联“未入库的条码的数量信息”
     *
     * @param p
     * @return
     */
    public List<SubcontractingOrderDetailVO> queryList4LabelAmount(SubcontractingOrderDetailVO p) {
        return subcontractingOrderDetailMapper.queryList4LabelAmount(p);
    }

    public Map<String, SubcontractingOrderDetailVO> queryList4LabelAmountMap(String fid) {
        SubcontractingOrderDetailVO p = new SubcontractingOrderDetailVO();
        p.setFid(fid);
        List<SubcontractingOrderDetailVO> ll = subcontractingOrderDetailMapper.queryList4LabelAmount(p);
        if (ll == null || ll.size() == 0) {
            return new HashMap<>();
        }
        return ll.stream().collect(Collectors.toMap(SubcontractingOrderDetailVO::getId, v -> v));
    }

    public Map<String, List<SubcontractingOrderDetailVO>> queryHeadMap(List<String> fids) {
        if (fids == null || fids.size() == 0) {
            return new HashMap<>();
        }
        Map<String, List<SubcontractingOrderDetailVO>> r = new HashMap<>();
        Lists.partition(fids, 1000).forEach(fids1 -> {
            Map<String, List<SubcontractingOrderDetailVO>> r1 = doQueryHeadMap(fids1);
            r.putAll(r1);
        });
        return r;
    }

    public Map<String, List<SubcontractingOrderDetailVO>> doQueryHeadMap(List<String> fids) {
        SubcontractingOrderDetailVO p = new SubcontractingOrderDetailVO();
        p.setFids(fids);
        List<SubcontractingOrderDetailVO> ll = queryList(p);
        if (ll == null || ll.size() == 0) {
            return new HashMap<>();
        }
        return ll.stream().collect(Collectors.groupingBy(SubcontractingOrderDetailVO::getFid));
    }

    public void batchInsert(List<SubcontractingOrderDetailDO> details) {
        if (details == null || details.size() == 0) {
            return;
        }
        for (SubcontractingOrderDetailDO detail : details) {
            subcontractingOrderDetailMapper.save(detail);
        }
    }

    public void updateNotNullByPrimaryKeySelective(SubcontractingOrderDetailDO inboundDetailDO) {
        subcontractingOrderDetailMapper.updateNotNullByPrimaryKeySelective(inboundDetailDO);
    }
}
